package com.mdl.pickerviewdemo

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.ScrollView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.CustomListener
import com.bigkoo.pickerview.listener.OnOptionsSelectListener
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.bigkoo.pickerview.view.TimePickerView
import com.contrarywind.view.WheelView
import com.mdl.pickerviewdemo.bean.CardBean
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.pickerview_custom_time.*
import java.text.SimpleDateFormat
import java.util.*

class MainActivity : AppCompatActivity() {
    var timePicker: TimePickerView? = null
    var leftText : Boolean = true
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //popupwindow
        showpop.setOnClickListener {
            var itemLayout = LayoutInflater.from(this).inflate(R.layout.popup_list_layout, null);
            var rv = itemLayout.findViewById<RecyclerView>(R.id.rv)
            val layoutParams = rv.layoutParams as (ConstraintLayout.LayoutParams)
            layoutParams.matchConstraintMaxHeight = 200
            rv.layoutParams = layoutParams
            rv.layoutManager = LinearLayoutManager(this)
            rv.adapter = RvAdapter()
            val popupWindow = PopupWindow(
                itemLayout,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                true
            )
            popupWindow.setBackgroundDrawable(ColorDrawable())
//            popupWindow.showAsDropDown(showpop)
            val rect = Rect()
            showpop.getGlobalVisibleRect(rect)
            val height: Int = showpop.getResources().getDisplayMetrics().heightPixels - rect.bottom
            popupWindow.height = height

            popupWindow.showAtLocation(showpop, Gravity.BOTTOM, 0, 0)
        }

        //普通一级滚轴
        btn_normal.setOnClickListener {
            val list = mutableListOf<CardBean>()
            list.add(CardBean(1, "红卡片"))
            list.add(CardBean(2, "黄卡片"))
            list.add(CardBean(3, "蓝卡片"))
            list.add(CardBean(4, "绿卡片"))
            list.add(CardBean(5, "黑卡片"))
            list.add(CardBean(6, "白卡片"))
            list.add(CardBean(7, "紫卡片"))
            val options = OptionsPickerBuilder(this, object : OnOptionsSelectListener {
                override fun onOptionsSelect(
                    options1: Int,
                    options2: Int,
                    options3: Int,
                    v: View?
                ) {
                    Log.d("optionstest", list.get(options1).content)
                }
            }).setTitleText("选择卡片")
                .setTitleColor(Color.BLUE)
                .setSubmitColor(Color.BLUE)
                .setCancelColor(Color.BLUE)
                .setDividerColor(Color.TRANSPARENT)
                .setTextColorCenter(Color.BLUE)
                .setTextColorOut(Color.GRAY)
                .setDividerType(WheelView.DividerType.FILL)
                .build<CardBean>()
            options.setPicker(list)
            options.show()
        }

        //两级联动滚轴
        btn_normal2.setOnClickListener {
            val list = mutableListOf<CardBean>()
            list.add(CardBean(1, "红卡片"))
            list.add(CardBean(2, "黄卡片"))
            list.add(CardBean(3, "蓝卡片"))
            val list2 = mutableListOf<MutableList<CardBean>>();
            var child1 = mutableListOf<CardBean>()
            child1.add(CardBean(1, "红色1"))
            child1.add(CardBean(2, "红色2"))
            child1.add(CardBean(3, "红色3"))
            var child2 = mutableListOf<CardBean>()
            child2.add(CardBean(1, "黄色1"))
            child2.add(CardBean(2, "黄色2"))
            var child3 = mutableListOf<CardBean>()
            child3.add(CardBean(1, "蓝色1"))

            list2.add(child1)
            list2.add(child2)
            list2.add(child3)
            val options = OptionsPickerBuilder(this, object : OnOptionsSelectListener {
                override fun onOptionsSelect(
                    options1: Int,
                    options2: Int,
                    options3: Int,
                    v: View?
                ) {
                    Log.d("optionstest", list.get(options1).content)
                }
            }).setTitleText("选择卡片二级联动")
                .setTitleColor(Color.BLUE)
                .setSubmitColor(Color.BLUE)
                .setCancelColor(Color.BLUE)
                .setDividerColor(Color.TRANSPARENT)
                .setTextColorCenter(Color.BLUE)
                .setTextColorOut(Color.GRAY)
                .build<CardBean>()
            options.setPicker(list, list2)
            options.show()
        }

        btn_normal3.setOnClickListener {
            val timePicker = TimePickerBuilder(this, object : OnTimeSelectListener {
                override fun onTimeSelect(date: Date?, v: View?) {

                }


            }).setTitleText("选择时间")
                .setTitleColor(Color.BLUE)
                .setSubmitColor(Color.BLUE)
                .setCancelColor(Color.BLUE)
                .setDividerColor(Color.TRANSPARENT)
                .setTextColorCenter(Color.BLUE)
                .setTextColorOut(Color.GRAY)
                .build()
            timePicker.show()
        }

        btn_normal4.setOnClickListener {
            val list = mutableListOf<CardBean>()
            list.add(CardBean(1, "00:00"))
            list.add(CardBean(2, "01:00"))
            list.add(CardBean(3, "02:00"))
            list.add(CardBean(4, "03:00"))
            list.add(CardBean(5, "04:00"))
            list.add(CardBean(6, "05:00"))
            list.add(CardBean(7, "06:00"))
            list.add(CardBean(8, "07:00"))
            list.add(CardBean(9, "08:00"))
            list.add(CardBean(10, "09:00"))
            val options = OptionsPickerBuilder(this, object : OnOptionsSelectListener {
                override fun onOptionsSelect(
                    options1: Int,
                    options2: Int,
                    options3: Int,
                    v: View?
                ) {
                    Log.d("optionstest", list.get(options1).content)
                }
            }).setTitleText("选择时间")
                .setTitleColor(Color.BLUE)
                .setSubmitColor(Color.BLUE)
                .setCancelColor(Color.BLUE)
                .setDividerColor(Color.TRANSPARENT)
                .setTextColorCenter(Color.BLUE)
                .setTextColorOut(Color.GRAY)
                .setDividerType(WheelView.DividerType.FILL)
                .setVisitToTv(true)
                .build<CardBean>()
            options.setNPicker(list, list, null);
            options.show()
        }

        btn_normal5.setOnClickListener {
//            this?.also { context ->
//                PrintHelper(context).apply {
//                    scaleMode = PrintHelper.SCALE_MODE_FIT
//                }.also { printHelper ->
////                    val bitmap = BitmapFactory.decodeResource(resources, R.drawable.droids)
//                    printHelper.printBitmap("droids.jpg - test print", getBitmapFromView(root))
//                }
//            }
        }

        selected1.setOnClickListener {
            selected1.isSelected = true
            selected2.isSelected = false
        }

        selected2.setOnClickListener {
            selected2.isSelected = true
            selected1.isSelected = false
        }

        btn_normal6.setOnClickListener {
            if (timePicker == null) {
                val startDate = Calendar.getInstance()
                startDate.set(2021,0,1)
                val endDate = Calendar.getInstance()
                endDate.set(2035,11,1)
                val tody = Calendar.getInstance()
                timePicker = TimePickerBuilder(this, object : OnTimeSelectListener {
                    override fun onTimeSelect(date: Date, v: View?) {

                    }
                }).setRangDate(startDate,endDate)
                    .setDate(tody)
                    .setLayoutRes(R.layout.pickerview_custom_time, object : CustomListener {
                    override fun customLayout(v: View) {
                        var startTv = v.findViewById<TextView>(R.id.start_tv)
                        var endTv = v.findViewById<TextView>(R.id.end_tv)
                        var tvCancle = v.findViewById<TextView>(R.id.tv_cancle)
                        var tv_finish = v.findViewById<TextView>(R.id.tv_finish)
                        tvCancle.setOnClickListener {
                            timePicker!!.dismiss()
                        }
                        tv_finish.setOnClickListener {
                            timePicker!!.dismiss()
                        }
                        startTv.setOnClickListener {
                            viewleft.visibility = View.VISIBLE
                            viewright.visibility = View.GONE
                            leftText = true
                        }
                        endTv.setOnClickListener {
                            viewleft.visibility = View.GONE
                            viewright.visibility = View.VISIBLE
                            leftText = false
                        }
                    }
                }).setTitleText("选择时间")
                    .setTitleColor(Color.BLUE)
                    .setSubmitColor(Color.BLUE)
                    .setCancelColor(Color.BLUE)
                    .setDividerColor(Color.TRANSPARENT)
                    .setTextColorCenter(Color.BLUE)
                    .setTextColorOut(Color.GRAY)
                    .setTimeSelectChangeListener(object : OnTimeSelectChangeListener {
                        override fun onTimeSelectChanged(date: Date) {
                            if (leftText){
                                start_tv.text = formateTime(date)
                            }else{
                                end_tv.text = formateTime(date)
                            }

                        }

                    })
                    .build()
            }
            timePicker?.show()

        }

        et.isFocusable = false

        et.setOnClickListener {
            if (isEdit){
                et.isFocusable = true
                et.isFocusableInTouchMode = true
                et.requestFocus()
                val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                imm.showSoftInput(et, 0)
            }
        }


        btn_normal7.setOnClickListener {
         isEdit = true
        }

        btn_normal8.setOnClickListener {
            isEdit = false
            val imm: InputMethodManager =
                getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm!!.hideSoftInputFromWindow(currentFocus?.windowToken,0)
            et.isFocusable = false
        }
    }

    var isEdit = false

    fun getBitmapFromView(v: ScrollView): Bitmap {
        var h = 0;
        var bitmap: Bitmap? = null
        for (i in 0..v.childCount - 1) {
            h += v.getChildAt(i).height
        }
        bitmap = Bitmap.createBitmap(v.width, h, Bitmap.Config.RGB_565)
        val canvas = Canvas(bitmap)
        canvas.drawColor(Color.WHITE)
        v.draw(canvas)
        return bitmap
    }


   fun formateTime (time:Date) : String{
       val format = SimpleDateFormat("yyyy-MM-dd")
       val datastr = format.format(time)
       return datastr
   }

    inner class RvAdapter : RecyclerView.Adapter<RvAdapter.ViewHolder>() {


        inner class ViewHolder(view:View):RecyclerView.ViewHolder(view){

        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            var view = LayoutInflater.from(parent.context).inflate(R.layout.item,parent,false)
            return ViewHolder(view)
        }

        override fun getItemCount(): Int {
            return 20
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        }


    }

}